package com.antdesignbackend.backend.news.noticeusers.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.antdesignbackend.backend.config.WebConfig;
import com.antdesignbackend.backend.news.newsusers.entity.entityVo.NewsUsersVo;
import com.antdesignbackend.backend.news.notice.entity.Notice;
import com.antdesignbackend.backend.news.notice.service.NoticeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.news.noticeusers.entity.NoticeUsers;
import com.antdesignbackend.backend.news.noticeusers.entity.entityVo.NoticeUsersVo;
import com.antdesignbackend.backend.news.noticeusers.service.NoticeUsersService;

/**
 * 通知公告
 *
 * @author 不二
 * @email NoticeUsers
 * @date 2020-07-26 21:31:12
 */

@Controller
@RequestMapping("/noticeUsers")
@Slf4j
public class NoticeUsersController extends BaseController{

	private static final String FUN_NAME = "通知公告";//功能名称

	@Autowired
	private NoticeUsersService noticeUsersService;
	@Autowired
	private NoticeService noticeService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "news/noticeUsers/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody NoticeUsersVo noticeUsersVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		List<NoticeUsersVo> list = new ArrayList<>();
		QueryWrapper<NoticeUsersVo> wrapper = new QueryWrapper<>();
		wrapper.eq("uid",getLoginUserId());
		if (noticeUsersVo.getStatus() != null && noticeUsersVo.getStatus() != -1){
			wrapper.eq("status",noticeUsersVo.getStatus());
		}
		if (noticeUsersVo.getType() != null && noticeUsersVo.getType() != -1){
			wrapper.eq("type",noticeUsersVo.getType());
		}
		if (noticeUsersVo.getTitle() != null && !noticeUsersVo.getTitle().equals("")){
			wrapper.like("title",noticeUsersVo.getTitle());
		}
		wrapper.orderByDesc("CreateTime");
		//查询总数
		int total = noticeUsersService.count(wrapper);
		if (total > 0){
			list = noticeUsersService.search(wrapper,new Page<NoticeUsersVo>(noticeUsersVo.getPageIndex(),noticeUsersVo.getPageSize()));
			if (list.size() > 0){
				for (NoticeUsersVo noticeUsersVo1:list){
					//去掉内容html标签，用于显示
					if (noticeUsersVo1.getContent() != null){
						String showContent = noticeUsersVo1.getContent().replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "");
						if (showContent.length() > 16){
							showContent = showContent.substring(0,16) + "...";
						}
						noticeUsersVo1.setContent(showContent);
					}
					//拼接文件路径
					if (noticeUsersVo1.getEnclosure()!=null && !noticeUsersVo1.getEnclosure().equals(""))
						noticeUsersVo1.setEnclosure(WebConfig.host + "enclosure/" + noticeUsersVo1.getEnclosure());
				}
			}
		}
		result.setTotal(total);
		result.setData(list);
		return result;

	}

	/**
	 * 查看公告 【修改状态为已读】
	 * @param id
	 * @return
	 */
	@GetMapping("/read/{id}")
	@ResponseBody
	public	ReturnVO read(@PathVariable("id") String id) {
		ReturnVO returnVO = new ReturnVO();
		NoticeUsers noticeUsers = noticeUsersService.getById(id);
		if (noticeUsers != null){
			if (noticeUsers.getStatus() == 0){
				noticeUsers.setStatus(1);
				noticeUsersService.updateById(noticeUsers);
			}
			Notice notice = noticeService.getById(noticeUsers.getNid());
			returnVO.setData(notice);
		}
		return returnVO;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody NoticeUsers noticeUsers){
		ReturnVO result = new ReturnVO();
		if(noticeUsersService.save(noticeUsers)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody NoticeUsers noticeUsers){
		ReturnVO result = new ReturnVO();
		if(noticeUsersService.updateById(noticeUsers)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (noticeUsersService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}



}
